<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>校园综合服务系统</title>
    <head>
        <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
        <script>
            $(document).ready(function(){
                var sto = window.localStorage.getItem("username")
                console.log(window.localStorage.getItem("username"))
                var data = $.parseJSON(sto) 
                $.get("http://localhost:80/edu/student/all-courses", {student_user_id: data.student_id}, function(response) {
                    var rs = $.parseJSON(response)
                    if (rs.code != "00000") {
                        alert("获取所有课程数据失败")
                    }
                    console.log(rs);
                    $.each(rs.data, function(index, course) {
                        // 创建表格行和各个单元格
                        var row = $("<tr></tr>");
                        row.append("<td>" + course.id + "</td>");
                        row.append("<td>" + course.course_name + "</td>");
                        row.append("<td>" + course.teacher_realname + "</td>");
                        row.append("<td>" + course.course_time + "</td>");
                        row.append("<td>" + course.course_code + "</td>");
                        row.append("<td>" + course.course_room + "</td>");
                        row.append("<td>" + course.textbook + "</td>");
                        row.append("<td>" + course.credit_count + "</td>");
                        row.append("<td>" + course.pass_score + "</td>");
                        row.append("<td>" + course.section_count + "</td>");
                        row.append("<td>" + course.max_student_count + "</td>");
                        row.append("<td>" + course.current_section_count + "</td>");
                        row.append("<td>" + course.course_year + "</td>");
                        row.append("<td>" + course.course_semester + "</td>");

                        // 创建补考按钮并附加点击事件
                        var scButton = $("<input type='button' value='选择课程' />");
                        scButton.click(function() {
                            $.post("http://localhost:80/edu/student/select-course", {student_user_id: data.student_id, course_code: course.course_code}, function(response) {
                                var rj = $.parseJSON(response)
                                if (rj.code != "00000") {
                                    alert(rj.message)
                                } else {
                                    alert("选课成功")
                                }
                            })
                        });

                        // 创建查看课程按钮并附加点击事件
                        var ccButton = $("<input type='button' value='取消选课' />");
                        ccButton.click(function() {
                            $.post("http://localhost/edu/student/cancel-course", {student_user_id: data.student_id, course_code: course.course_code}, function(response) {
                                var rj = $.parseJSON(response)
                                if (rj.code != "00000") {
                                    alert(rj.message)
                                } else {
                                    alert("取消选课成功")
                                }
                            })
                        });

                        // 将按钮添加到表格行中
                        var cell = $("<td></td>");
                        cell.append(scButton);
                        cell.append(ccButton);
                        row.append(cell);

                        // 将表格行添加到表格中
                        $("#mycourses").append(row);
                    });

                    
                });
            });
        </script>
        </head>
    <style>
        .container {
            text-align: center;
        }
    </style>
</head>
<body>
    
    <a href="user_courses.html">我的课程</a><br>
    <a href="all_courses.html">选课</a><br>
    <a href="all_blogs.html">校园论坛</a><br>
    <a href="change_pwd.html">修改密码</a><br>
    <a href="teacher_schedule.html">教师课表</a><br>
    <a href="student_schedule.html">学生课表</a><br>
    <div class="container">    
        <table id="mycourses">
            <thead>
              <tr>
                <th>序号</th>
                <th>课程名称</th>
                <th>任课教师</th>
                <th>课程时间</th>
                <th>课程编号</th>
                <th>课程教室</th>
                <th>教材</th>
                <th>学分</th>
                <th>及格分数</th>
                <th>课时数量</th>
                <th>最大选课人数</th>
                <th>当前课程人数</th>
                <th>课程学年</th>
                <th>课程学期</th>
                <th>操作</th>
              </tr>
            </thead>
            <tbody>
              
            </tbody>
          </table>
    </div>
    


</body>
</html>